package xyz.zhouzhaodong.crm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import xyz.zhouzhaodong.crm.dto.NoticeDto;
import xyz.zhouzhaodong.crm.entity.Notice;
import xyz.zhouzhaodong.crm.mapper.NoticeMapper;
import xyz.zhouzhaodong.crm.service.INoticeService;
import xyz.zhouzhaodong.crm.util.RestMessage;

import javax.annotation.Resource;
import java.beans.Transient;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zhouzhaodong
 * @since 2020-06-15
 */
@Service
public class NoticeServiceImpl extends ServiceImpl<NoticeMapper, Notice> implements INoticeService {

    @Resource
    NoticeMapper noticeMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestMessage addNotice(NoticeDto noticeDto) {
        try {
            Notice notice = noticeDtoToNotice(noticeDto);
            // 新增公告
            String id = UUID.randomUUID().toString().replaceAll("-", "");
            notice.setId(id);
            // 这里需要根据部门或者岗位查出所有的用户
            String department = notice.getReceiveDepartment();
            String[] departmentStr = department.split(",");
            List<String> list = noticeMapper.selectStaffIdByDepartment(departmentStr);
            StringBuilder stringBuilder = new StringBuilder();
            for (String str : list) {
                stringBuilder.append(str).append(",");
            }
            if (stringBuilder.length() > 1) {
                String staffIds = stringBuilder.substring(0, stringBuilder.length() - 1);
                notice.setReceiver(staffIds);
            }
            notice.setUpdateUser(noticeDto.getStaffId());
            notice.setUpdateTime(new Date());
            noticeMapper.insert(notice);
            return RestMessage.success(id);
        } catch (Exception e) {
            return RestMessage.failed(e.getMessage());
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestMessage deleteNotice(NoticeDto noticeDto) {
        try {
            Notice notice = new Notice();
            // 删除公告
            notice.setId(noticeDto.getId());
            notice.setUpdateUser(noticeDto.getStaffId());
            notice.setUpdateTime(new Date());
            notice.setFlag(1);
            noticeMapper.updateById(notice);
            return RestMessage.success();
        } catch (Exception e) {
            return RestMessage.failed(e.getMessage());
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestMessage updateNotice(NoticeDto noticeDto) {
        try {
            Notice notice = noticeDtoToNotice(noticeDto);
            // 更新公告
            notice.setUpdateUser(noticeDto.getStaffId());
            notice.setUpdateTime(new Date());
            noticeMapper.updateById(notice);
            return RestMessage.success();
        } catch (Exception e) {
            return RestMessage.failed(e.getMessage());
        }
    }

    @Override
    public RestMessage selectNoticeByStaffId(NoticeDto noticeDto) {
        // 根据用户id查询公告
        QueryWrapper<Notice> queryWrapper = new QueryWrapper<>();
        if(noticeDto.getFlag() == null){
            queryWrapper.eq("flag", 0);
        } else {
            queryWrapper.eq("flag", noticeDto.getFlag());
        }
        queryWrapper.like("receiver", noticeDto.getStaffId());
        Page<Notice> page = new Page<>(noticeDto.getPageNo(),
                noticeDto.getPageSize() == 0 ? 10 : noticeDto.getPageSize());
        IPage<Notice> list = noticeMapper.selectPage(page, queryWrapper);
        return RestMessage.success(list);
    }

    @Override
    public RestMessage selectNotice(NoticeDto noticeDto) {
        // 如果有查询条件需要
        QueryWrapper<Notice> queryWrapper = new QueryWrapper<>();
        Integer flag = noticeDto.getFlag();
        if (flag == null) {
            flag = 0;
        }
        queryWrapper.eq("flag", flag);
        String id = noticeDto.getId();
        if (id != null && !"".equals(id)) {
            queryWrapper.eq("id", id);
        }
        String receiveDepartment = noticeDto.getReceiveDepartment();
        if (receiveDepartment != null && !"".equals(receiveDepartment)) {
            queryWrapper.like("receive_department", receiveDepartment);
        }
        Page<Notice> page = new Page<>(noticeDto.getPageNo(),
                noticeDto.getPageSize() == 0 ? 10 : noticeDto.getPageSize());
        IPage<Notice> list = noticeMapper.selectPage(page, queryWrapper);
        return RestMessage.success(list);
    }

    public Notice noticeDtoToNotice(NoticeDto noticeDto) {
        Notice notice = new Notice();
        notice.setId(noticeDto.getId());
        notice.setTitle(noticeDto.getTitle());
        notice.setContent(noticeDto.getContent());
        notice.setReceiver(noticeDto.getReceiver());
        notice.setReceiveStatus(noticeDto.getReceiveStatus());
        notice.setReceiveDepartment(noticeDto.getReceiveDepartment());
        notice.setFlag(noticeDto.getFlag());
        return notice;
    }
}
